package com.example.simpleforum.servlet;

import com.example.simpleforum.dao.ForumDao;
import com.example.simpleforum.dao.PostDao;
import com.example.simpleforum.model.Forum;
import com.example.simpleforum.model.Post;
import com.example.simpleforum.model.User;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;

/**
 * 发布帖子Servlet
 */
public class PostAddServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 获取所有板块
        ForumDao forumDao = new ForumDao();
        List<Forum> forums = forumDao.findAll();

        // 将板块列表存入请求属性
        request.setAttribute("forums", forums);

        // 转发到发布帖子页面
        request.getRequestDispatcher("/postAdd.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 获取当前登录用户
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        if (user == null) {
            // 用户未登录，重定向到登录页面
            response.sendRedirect(request.getContextPath() + "/login.jsp");
            return;
        }

        // 获取表单数据
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        String forumIdStr = request.getParameter("forumId");

        // 表单验证
        if (title == null || title.trim().isEmpty()) {
            request.setAttribute("error", "标题不能为空");
            doGet(request, response);
            return;
        }

        if (content == null || content.trim().isEmpty()) {
            request.setAttribute("error", "内容不能为空");
            doGet(request, response);
            return;
        }

        if (forumIdStr == null || forumIdStr.trim().isEmpty()) {
            request.setAttribute("error", "请选择板块");
            doGet(request, response);
            return;
        }

        int forumId = Integer.parseInt(forumIdStr);

        // 创建帖子对象
        Post post = new Post();
        post.setTitle(title);
        post.setContent(content);
        post.setUserId(user.getId());
        post.setForumId(forumId);
        post.setCreateTime(new Timestamp(System.currentTimeMillis()));
        post.setUpdateTime(post.getCreateTime());

        // 保存帖子
        PostDao postDao = new PostDao();
        int postId = postDao.addPost(post);

        if (postId > 0) {
            // 发布成功，重定向到帖子详情页面
            response.sendRedirect(request.getContextPath() + "/PostDetailServlet?id=" + postId);
        } else {
            // 发布失败
            request.setAttribute("error", "发布失败，请稍后再试");
            doGet(request, response);
        }
    }
}
